//
//  MJStatusOriginalView.swift
//  新浪微博
//
//  Created by MaJi on 16/4/12.
//  Copyright © 2016年 MaJi. All rights reserved.
//
import UIKit
import SnapKit
import SDWebImage
let StatusCellMargin:CGFloat = 8
class MJStatusOriginalView: UIView {
//记录底部约束
    var bottomCons: Constraint?
//    绑定数据模型
    var statusViewModel:MJststusOriganalVIewModel? {
        didSet{
//            设置头像
            iconView.sd_setImageWithURL(statusViewModel?.iconUrl)
            //        设置name
            nameLabel.text = statusViewModel?.status?.user?.name
//            设置用户等级
            mbRankImageView.image = statusViewModel?.mbImage
//            用户认证类型
            verifiedImageView.image = statusViewModel?.verifiedImage
//            时间
            timeLabel.text = statusViewModel?.time
//            微博来源
            sourceLabel.text = statusViewModel?.source
//            微博正文
            contentLabel.text = statusViewModel?.status?.text
//            判断是否有图片
            //在更新约束之前先 卸载self的底部约束
            self.bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count>0{
//                有图片.更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(picView.snp_bottom).offset(StatusCellMargin).constraint
                })
//            给照片view绑定模型
                picView.statusPicture = urls
                
//                显示view
                picView.hidden = false
            }else{
//                没有图片.更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
//                绑定模型
                picView.statusPicture = nil
//                隐藏view
                picView.hidden = true
            }

        }

    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addSubview(topView)
        topView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(iconView)
        addSubview(mbRankImageView)
        addSubview(verifiedImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(picView)
//        设置约束
        topView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(5)
        }
        iconView.cornerRadius = 20
        iconView.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        mbRankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            
        }
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconView.snp_right)
            make.bottom.equalTo(iconView.snp_bottom)
            
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
            
        }
//        图片
        picView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.left.equalTo(self).offset(StatusCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
//        设置view底部,相对与微博正文或图片的约束
//        if statusViewModel?.status?.pic_urls?.count>0 {
//            有图片
//          显示图片
            picView.hidden = false
            self.snp_makeConstraints { (make) -> Void in
                self.bottomCons = make.bottom.equalTo(picView.snp_bottom).offset(StatusCellMargin).constraint
//            }
//        }else{
////            隐藏图片
//            picView.hidden = true
//            self.snp_makeConstraints { (make) -> Void in
//                make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
//            }
            
        }
    }
    
//    懒加载 子控件
//    用户头像
    private lazy var topView:UIView = UIView()
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
//    用户等级
    private lazy var mbRankImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
//    用户认证类型
    private lazy var verifiedImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
//    用户名 (给UILabel写延展)
    private lazy var nameLabel:UILabel = UILabel(text: "马季", textColor: UIColor.darkGrayColor(), fontSize: 15, margin: StatusCellMargin)
//    微博时间
    private lazy var timeLabel:UILabel = UILabel(text: "15:15", textColor: UIColor.darkGrayColor(), fontSize: 12, margin: StatusCellMargin)
//    微博来源
    private lazy var sourceLabel:UILabel = UILabel(text: "来自隔壁老王", textColor: UIColor.darkGrayColor(), fontSize: 12, margin: StatusCellMargin)
//    微博正文
    private lazy var contentLabel:UILabel = UILabel(text: "微博正文", textColor: UIColor.darkGrayColor(), fontSize: 12, margin: StatusCellMargin)
//    图片
    private lazy var picView:MJStatuePictureView = MJStatuePictureView()
}
